/*
    doc.me -- MakeMe file for ESP documentation 
 */

Me.load({

    targets: {
        api: {
            depends: ['render-doc'],
            action: `
                rm(['api/edi.tags', 'api/esp.tags'])
                let tags = me.dir.paks.join('*/doc/api/*.tags')
                apidoc('api/edi.dox', ['${SRC}/edi.h', '${SRC}/mdb.h' ], 'EDI', tags)
                apidoc('api/esp.dox', ['${SRC}/esp.h'], 'ESP', [tags, 'api/edi.tags'])
                let files = Path().files(['api/esp.html', 'api/edi.html'])
                for each (let src in files) {
                    apiLayout(src, Path('dist/ref').join(src))
                }

                let base = me.dir.paks.join('*/doc')
                let files = Path().files(base.join('api/*.html'))
                for each (src in files) {
                    let dest = src.trimComponents(base.components.length)
                    apiLayout(src, Path('dist/ref').join(dest))
                }
            `,
        },
        validate: {
            depends: [ 'gen-doc' ],
            action: `
                for each (f in Path('public').files('**/*.html')) {
                    let data = f.readString()
                    data = data.replace('700|Open', '').
                        replace(/esp.download.html/g, 'download/esp.html').
                        replace(/appweb.download.html/g, 'download/appweb.html').
                        replace(/developers.contributors.html/g, 'scripts/contributors.esp').
                        replace(/pak.download.html/g, 'download/pak.html').
                        replace(/nowrap="nowrap"/g, '').
                        replace(/nowrap/g, '').
                        replace(/https:..embedthis.com.appweb.licensing.html/g, 'https://embedthis.com/').
                        replace(/https:..embedthis.com.appweb.doc.users.configuration.html/g, 'https://embedthis.com/').
                        replace(/https:..embedthis.com.appweb.doc.users.dir.esp.html/g, 'https://embedthis.com/').
                        replace(/https:..embedthis.com.appweb.doc.users.dir.route.html/g, 'https://embedthis.com/').
                        replace(/https:..embedthis.com.esp.licensing.html/g, 'https://embedthis.com/').
                        replace(/https:..embedthis.com.makeme.doc.guide.me.source.building.htm/g, 'https://embedthis.com/').
                        replace(/https:..embedthis.com.makeme.doc.source.building.htm/g, 'https://embedthis.com/').
                        replace(/https:..embedthis.com.makeme.doc.source/g, 'https://embedthis.com/').
                        replace(/https:..embedthis.com.developers./g, 'https://embedthis.com/')
                    trace('Patch', f)
                    f.write(data)
                }
                for each (f in Path('public/man').files('*.html')) {
                    f.write(f.readString().toLowerCase())
                }
                trace('Listen', 'expansive --nowatch')
                run('expansive --nowatch')
            `
        },
    },
})
